
package org.jeecg.modules.flowable.converter;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.StartEvent;
import org.flowable.editor.language.json.converter.BaseBpmnJsonConverter;
import org.flowable.editor.language.json.converter.BpmnJsonConverterContext;
import org.flowable.editor.language.json.converter.StartEventJsonConverter;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**********************************************************
 * <b>CustomStartEventJsonConverter.java</b></br>
 *
 * <pre>开始节点获取模型公共属性</pre>
 *
 * @author rjgf
 * @date 2021-09-14 16:43:09
 **********************************************************/
public class CustomStartEventJsonConverter extends StartEventJsonConverter {

    public static void customFillTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap, Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {
        fillJsonTypes(convertersToBpmnMap);
        fillBpmnTypes(convertersToJsonMap);
    }

    public static void fillJsonTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap) {
        convertersToBpmnMap.put(STENCIL_EVENT_START_NONE, CustomStartEventJsonConverter.class);
        convertersToBpmnMap.put(STENCIL_EVENT_START_TIMER, CustomStartEventJsonConverter.class);
        convertersToBpmnMap.put(STENCIL_EVENT_START_CONDITIONAL, CustomStartEventJsonConverter.class);
        convertersToBpmnMap.put(STENCIL_EVENT_START_ERROR, CustomStartEventJsonConverter.class);
        convertersToBpmnMap.put(STENCIL_EVENT_START_ESCALATION, CustomStartEventJsonConverter.class);
        convertersToBpmnMap.put(STENCIL_EVENT_START_MESSAGE, CustomStartEventJsonConverter.class);
        convertersToBpmnMap.put(STENCIL_EVENT_START_EVENT_REGISTRY, CustomStartEventJsonConverter.class);
        convertersToBpmnMap.put(STENCIL_EVENT_START_SIGNAL, CustomStartEventJsonConverter.class);
    }

    public static void fillBpmnTypes(Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {
        convertersToJsonMap.put(StartEvent.class, CustomStartEventJsonConverter.class);
    }


    @Override
    protected void convertElementToJson(ObjectNode propertiesNode, BaseElement baseElement, BpmnJsonConverterContext converterContext) {
        super.convertElementToJson(propertiesNode, baseElement, converterContext);
        if (!(baseElement instanceof StartEvent)) {
            return;
        }
        Map<String, String> map = new HashMap<>(2);
        baseElement.getExtensionElements().forEach((s, elements) -> elements.forEach(extensionElement -> map.put(extensionElement.getName(), extensionElement.getElementText())));
        if (CollectionUtils.isEmpty(map)) {
            return;
        }
        map.forEach(propertiesNode::put);
    }

    @Override
    protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode, Map<String, JsonNode> shapeMap, BpmnJsonConverterContext converterContext) {
        FlowElement flowElement = super.convertJsonToElement(elementNode, modelNode, shapeMap, converterContext);
        addExtensionElement(flowElement, elementNode);
        return flowElement;
    }


    public static void addExtensionElement(FlowElement flowElement, JsonNode elementNode) {
        // 暂无使用
    }
}
